﻿using System;
using System.Reflection;
using System.Runtime.InteropServices;
using HYElectricCommon;
using Microsoft.Office.Interop.Word;

namespace HYRElectric.LightningCalculation
{
	public class CalculateReport
	{
		public CalculateReport(CalculationData calculateData)
		{
			this.m_CalculateData = calculateData;
		}

		public bool ExportReport()
		{
			Microsoft.Office.Interop.Word.Application application = (Microsoft.Office.Interop.Word.Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("000209FF-0000-0000-C000-000000000046")));
			object value = Missing.Value;
			object obj = AssistFunc.GetAppRootPath() + "\\Sample\\年预计雷击次数计算书.dot";
			Microsoft.Office.Interop.Word._Document document = application.Documents.Add(ref obj, ref value, ref value, ref value);
			application.Visible = true;
			document.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
			object styleBT = "标题 1";
			object styleBT2 = "标题 2";
			object styleBT3 = "正文";
			string text = "  ";
			this.DrawRow(application, "年预计雷击次数计算书", styleBT);
			this.DrawRow(application, "1.计算依据:", styleBT2);
			this.DrawRow(application, "参考规范：《建筑物防雷设计规范》GB50057―2010", styleBT3);
			this.DrawRow(application, "2.已知条件:", styleBT2);
			if (this.m_CalculateData.IsFormulaMethod)
			{
				this.DrawRow(application, string.Concat(new string[]
				{
					text,
					text,
					"建筑物的长度L = ",
					this.m_CalculateData.BuildingLength.ToString("f2"),
					"m"
				}), styleBT3);
				this.DrawRow(application, string.Concat(new string[]
				{
					text,
					text,
					"建筑物的宽度W = ",
					this.m_CalculateData.BuildingWidth.ToString("f2"),
					"m"
				}), styleBT3);
				this.DrawRow(application, string.Concat(new string[]
				{
					text,
					text,
					"建筑物的高度H = ",
					this.m_CalculateData.BuildingHeight.ToString("f2"),
					"m"
				}), styleBT3);
			}
			else
			{
				this.DrawRow(application, text + text + "模型分析建筑物等效面积Ae为: " + this.m_CalculateData.Ae.ToString(), styleBT3);
			}
			this.DrawRow(application, string.Concat(new string[]
			{
				text,
				text,
				"当地的年平均雷暴日天数Td = ",
				this.m_CalculateData.Td.ToString(),
				"天/年"
			}), styleBT3);
			this.DrawRow(application, (text + text + "校正系数k =  " + this.m_CalculateData.K.ToString()) ?? "", styleBT3);
			if (this.m_CalculateData.IsFormulaMethod)
			{
				this.DrawRow(application, text + text + this.m_CalculateData.GetBuildingEnvironmentalFactors() + "。", styleBT3);
			}
			this.DrawRow(application, "3.计算公式:", styleBT2);
			this.DrawRow(application, text + text + "年预计雷击次数: N = k*Ng*Ae =  " + this.m_CalculateData.N.ToString(), styleBT3);
			this.DrawRow(application, string.Concat(new string[]
			{
				text,
				text,
				"其中: 建筑物的雷击大地的年平均密度: Ng = 0.1*Td = 0.1*",
				this.m_CalculateData.Td.ToString(),
				" = ",
				this.m_CalculateData.Ng.ToString()
			}), styleBT3);
			if (this.m_CalculateData.IsFormulaMethod)
			{
				foreach (string text2 in this.m_CalculateData.AeCalculateFormulaDetailed)
				{
					this.DrawRow(application, string.Concat(new string[]
					{
						text,
						text,
						text,
						text,
						text2
					}), styleBT3);
				}
			}
			this.DrawRow(application, "4.计算结果:", styleBT2);
			this.DrawRow(application, string.Concat(new string[]
			{
				text,
				text,
				"根据《防雷设计规范》，该建筑应该属于",
				this.m_CalculateData.ProtectionCategory,
				"建筑。"
			}), styleBT3);
			this.DrawRow(application, text + text + "附录:", styleBT3);
			this.DrawRow(application, text + text + "二类:N>0.05 省部级办公建筑和其他重要场所、人员密集场所 。", styleBT3);
			this.DrawRow(application, text + text + text + "N>0.25 住宅、办公楼等一般性民用建筑物或一般性工业建筑。", styleBT3);
			this.DrawRow(application, text + text + "三类:0.01<=N<=0.05 省部级办公建筑和其他重要场所、人员密集场所。", styleBT3);
			this.DrawRow(application, text + text + text + "0.05<=N<=0.25 住宅、办公楼等一般性民用建筑物或一般性工业建筑。", styleBT3);
			return true;
		}

		private void DrawRow(Microsoft.Office.Interop.Word.Application wordApp, string str, object styleBT)
		{
			wordApp.Selection.TypeText(str);
			wordApp.Selection.ParagraphFormat.set_Style(ref styleBT);
			wordApp.Selection.TypeParagraph();
		}

		private CalculationData m_CalculateData;
	}
}
